import 'package:flutter/material.dart';
import 'package:flutter_app/utils/windowUtil.dart';
import 'MainWidget1.dart';
import 'package:shared_preferences/shared_preferences.dart';
//import 'package:fluttertoast/fluttertoast.dart';
class Login extends StatefulWidget {
  @override
  State<Login> createState() => _LoginState();
}

class _LoginState extends State<Login> {
  TextEditingController userTextCon;
  TextEditingController pwdTextCon;
  @override
  void initState() {
    super.initState();
    userTextCon=TextEditingController();
    pwdTextCon=TextEditingController();
    getPre();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: new Container(

        color: Color (0xffffffff),
        child: Column(
          
          children: <Widget>[
            Stack(
              alignment: Alignment.center,
              children: <Widget>[
                Image.asset("images/log.jpg",width: 150.0,height: 70.0,)
                ,
                Column(
                  children: <Widget>[

                    SizedBox(height: 200.0,),
                    Text("",style: TextStyle(color: Color(0xffffffff)),)
                    
                  ],
                  
                )
                

              ],

            ),
            SizedBox(height: 30.0,),
            Container(margin:
              EdgeInsets.only(left: 30.0,right: 20.0)
              ,
            child:TextField(
              controller: userTextCon,
              keyboardType: TextInputType.numberWithOptions(),
              decoration: InputDecoration(hintText: "请输入手机号/邮箱",hintStyle:TextStyle(color: Color(0xffededed)),border: InputBorder.none),)
              ,

            ),
            Container(margin: EdgeInsets.only(left: 20.0,right: 20.0),height: 1.0,color: Color(0xffededed),),
            Container(margin:
              EdgeInsets.only(left: 30.0,right: 20.0)
              ,
            child:TextField(
              controller: pwdTextCon,
              keyboardType: TextInputType.numberWithOptions(),
              obscureText: true,
              decoration: InputDecoration(hintText: "请输入密码",hintStyle:TextStyle(color: Color(0xffededed)),border: InputBorder.none),)
              ,

            ),
            Container(margin: EdgeInsets.only(left: 20.0,right: 20.0),height: 1.0,color: Color(0xffededed),

            ),
            Container(
              height: 50.0,
              width: windowUtil.width-40.0,
//              decoration: BoxDecoration(
//                borderRadius: BorderRadius.circular(10.0)
//              ),
              margin: EdgeInsets.only(left: 20.0,right: 20.0,top: 10.0),
              child: RaisedButton(

                onPressed: (){
//                  Navigator.push(context, new MaterialPageRoute(builder: (BuildContext context){
//                    return new MainWidget1();
//                  }));
                if(userTextCon.text.trim().isEmpty){
//                  Fluttertoast.showToast(msg: "用户名不能为空");
                  return;
                }
                if(pwdTextCon.text.trim().isEmpty){
//                  Fluttertoast.showToast(msg: "密码不能为空");
                  return;
                }
                setPre(userTextCon.text,pwdTextCon.text).then((value){
                  Navigator.pushReplacement(context, new MaterialPageRoute(builder: (c){
                    return new MainWidget1();
                  }));

                });


              }, child: Text("登录",style: TextStyle(color: Color(0xffffffff),fontSize: 19.0),),color: Color(0xffFFB6C1),),),
            Container(
              height: 20.0,
              margin: EdgeInsets.only(left: 20.0,right: 20.0,top: 5.0),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: <Widget>[
                  Text("找回密码",style: TextStyle(color: Color(0xffFFB6C1)),),
                  Text("注册新用户",style: TextStyle(color: Color(0xffFFB6C1)),),

                ],),
            ),
            Expanded(child: Text("")),
            Container(
              height: 20.0,
                margin: EdgeInsets.only(left: 20.0,right: 20.0,bottom: 20.0),

            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
              Text("登录即表示阅读并已同意"),
              Text("服务条款",style: TextStyle(color: Color(0xffFFB6C1)),)

            ],),
            )



          ],

        ),
      ),
      resizeToAvoidBottomPadding: false,
    );
  }
  Future<void> setPre(String userName,String pwd) async{
          SharedPreferences sharedPreferences=await SharedPreferences.getInstance();
          sharedPreferences.setString("userName", userName);
          sharedPreferences.setString("pwd", pwd);
          print("set>");
  }
  void getPre() async{

    SharedPreferences sharedPreferences=await SharedPreferences.getInstance();
   String name= sharedPreferences.get("userName");
   String pwd= sharedPreferences.get("pwd");
   if(name.isEmpty||pwd.isEmpty){
     return;
   }
   userTextCon.text=name;
   pwdTextCon.text=pwd;
   setState(() {

   });
  }


}